package org.zecontech.extraplus.datagen;

import net.minecraft.data.PackOutput;
import net.minecraft.world.item.BlockItem;
import net.neoforged.neoforge.common.data.LanguageProvider;
import org.zecontech.extraplus.ExtraPlus;
import org.zecontech.extraplus.init.ModBlocks;
import org.zecontech.extraplus.init.ModEntities;
import org.zecontech.extraplus.init.ModItems;
import org.zecontech.extraplus.util.Helpers;

/**
 * 模组英文翻译数据文件提供者
 */
public class ModLanguageProviderEn extends LanguageProvider {

    public ModLanguageProviderEn(PackOutput output) {
        super(output, ExtraPlus.MOD_ID, "en_us");
    }

    /**
     * 添加翻译
     */
    @Override
    protected void addTranslations() {
        ModItems.ITEMS.getEntries().forEach(itemDeferredHolder -> {
            if (!(itemDeferredHolder.get() instanceof BlockItem)) {
                add(itemDeferredHolder.get(),
                        formatFieldName(Helpers.getItemName(itemDeferredHolder.get())));
            }
        });
        ModBlocks.BLOCKS.getEntries().forEach(blockDeferredHolder -> {
            add(blockDeferredHolder.get(),
                    formatFieldName(Helpers.getBlockName(blockDeferredHolder.get())));
        });
        ModEntities.ENTITIES.getEntries().forEach(entityTypeDeferredHolder -> {
            add(entityTypeDeferredHolder.get(), formatFieldName(Helpers.getEntityName(entityTypeDeferredHolder.get())));
        });
        add("itemGroup.extraplus.misc", "Extra Plus | Misc");
        add("itemGroup.extraplus.tools", "Extra Plus | Tools");
        add("itemGroup.extraplus.combat", "Extra Plus | Combat");
        add("itemGroup.extraplus.food", "Extra Plus | Food");
    }

    /**
     * 标准化内容
     * @param input 输入
     * @return 大小写规范的字符串字段
     */
    private static String formatFieldName(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }

        StringBuilder result = new StringBuilder();
        boolean nextUpper = true;

        for (char c : input.toCharArray()) {
            if (c == '_') {
                result.append(' ');
                nextUpper = true;
            } else {
                if (nextUpper) {
                    result.append(Character.toUpperCase(c));
                    nextUpper = false;
                } else {
                    result.append(Character.toLowerCase(c));
                }
            }
        }

        return result.toString();
    }
}
